package com.dz.risk.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dz.risk.common.ExcelExport;
import com.dz.risk.common.ExcelUtils;
import com.dz.risk.common.ResponseVO;
import com.dz.risk.dao.RiskFactorDao;
import com.dz.risk.pojo.domain.RiskFactorEntity;
import com.dz.risk.service.RiskFactorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author baohan
 * @className TemplateExpController
 * @description 模板导出
 * @date 2021/6/23 9:14
 */

@RestController
@RequestMapping("dz-ftsp/stest/template")
@Api(tags = "模板导出api", value = "模板导出")
public class TemplateExpController {

    @Autowired
    RiskFactorDao riskFactorDao;

    public static final String EXCEL_NAME_FAC = "风险因子.xlsx";
    public static final String SHEET_NAME = "风险因子";
    public static final String YYYYMMDD = "yyyyMMdd";

    @ApiOperation("导出风险因子值表")
    @GetMapping(value = "/exporFactorValue")
    public void exporFactor(HttpServletResponse response) {

        String[] headers = new String[]{"压力情景参数（股市跌债市跌）", "未来1个月", "未来2个月", "未来3个月"};
        String[] secondHeaders = new String[]{"压力情景参数（股市跌债市跌）", "轻度", "中度", "重度", "轻度", "中度", "重度", "轻度", "中度", "重度"};
        QueryWrapper<RiskFactorEntity> query = new QueryWrapper<>();
        List<RiskFactorEntity> riskFactorEntityList = riskFactorDao.selectList(query);
        try {
            ExcelExport.export(SHEET_NAME, headers, secondHeaders, riskFactorEntityList, EXCEL_NAME_FAC, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("导出风险因子表")
    @GetMapping(value = "/exporFactor")
    public void outPutExcel(HttpServletResponse response) throws Exception {
        SXSSFWorkbook wb = new SXSSFWorkbook();
        Sheet sh = wb.createSheet();
        QueryWrapper<RiskFactorEntity> query = new QueryWrapper<>();
        query.orderByAsc("risk_code");
        List<RiskFactorEntity> riskFactorEntityList = riskFactorDao.selectList(query);
        String[] titles = {"编码", "风险因子"};
        Row row = sh.createRow(0);
        CellStyle style = wb.createCellStyle();
        style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);

        // 第一行设置标题
        for (int i = 0; i < titles.length; i++) {
            String title = titles[i];
            Cell cell1 = row.createCell(i);
            cell1.setCellValue(title);
            cell1.setCellStyle(style);
        }

        sh.setColumnWidth(0, 6000);
        sh.setColumnWidth(1, 6000);


        // 导出数据
        for (int rowNum = 0; rowNum < riskFactorEntityList.size(); rowNum++) {
            Row rowData = sh.createRow(rowNum + 1);
            RiskFactorEntity tmp = riskFactorEntityList.get(rowNum);
            // 第一列
            Cell cellDataA = rowData.createCell(0);
            cellDataA.setCellValue(tmp.getRiskCode() + "");
            cellDataA.setCellStyle(style);
            // 第二列
            Cell cellDataB = rowData.createCell(1);
            cellDataB.setCellValue(tmp.getRiskContent());
            cellDataB.setCellStyle(style);
        }
        String fileName = "风险因子.xlsx";
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        wb.write(response.getOutputStream());
        wb.close();
    }

    @ApiOperation("导出预警债券名单")
    @GetMapping(value = "/exporBondRiskInformationSheet")
    public void exporBondRiskInformationSheet(HttpServletResponse response) throws Exception {
        SXSSFWorkbook wb = new SXSSFWorkbook();
        Sheet sh = wb.createSheet();
        //类型	债券	代码	违约开始日期	违约结束日期
        String[] titles = {"类型", "债券","代码","违约开始日期","违约结束日期"};
        Row row = sh.createRow(0);
        CellStyle style = wb.createCellStyle();
        style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);

        // 第一行设置标题
        for (int i = 0; i < titles.length; i++) {
            String title = titles[i];
            Cell cell1 = row.createCell(i);
            cell1.setCellValue(title);
            cell1.setCellStyle(style);
        }

        sh.setColumnWidth(0, 6000);
        sh.setColumnWidth(1, 6000);
        sh.setColumnWidth(2, 6000);
        sh.setColumnWidth(3, 10000);
        sh.setColumnWidth(4, 10000);
        String fileName = "预警债券名单.xlsx";
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        wb.write(response.getOutputStream());
        wb.close();
    }

    @ApiOperation("导出编码与中文管理")
    @GetMapping(value = "/exporIndicatordetails")
    public void exporIndicatordetails(HttpServletResponse response) throws Exception {
        SXSSFWorkbook wb = new SXSSFWorkbook();
        Sheet sh = wb.createSheet();
        //指标编号	指标名称
        String[] titles = {"指标编号", "指标名称"};
        Row row = sh.createRow(0);
        CellStyle style = wb.createCellStyle();
        style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);

        // 第一行设置标题
        for (int i = 0; i < titles.length; i++) {
            String title = titles[i];
            Cell cell1 = row.createCell(i);
            cell1.setCellValue(title);
            cell1.setCellStyle(style);
        }

        sh.setColumnWidth(0, 6000);
        sh.setColumnWidth(1, 6000);
        String fileName = "编码与中文管理.xlsx";
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        wb.write(response.getOutputStream());
        wb.close();
    }


    @ApiOperation("导出公式模板")
    @GetMapping(value = "/exportFormula")
    public void exportFormula(HttpServletResponse response) throws Exception {
        SXSSFWorkbook wb = new SXSSFWorkbook();
        Sheet sh = wb.createSheet();
        //公式编号	公式详细	备注
        String[] titles = {"公式编号", "公式详细","备注"};
        Row row = sh.createRow(0);
        CellStyle style = wb.createCellStyle();
        style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);

        // 第一行设置标题
        for (int i = 0; i < titles.length; i++) {
            String title = titles[i];
            Cell cell1 = row.createCell(i);
            cell1.setCellValue(title);
            cell1.setCellStyle(style);
        }

        sh.setColumnWidth(0, 6000);
        sh.setColumnWidth(1, 6000);
        sh.setColumnWidth(2, 6000);
        String fileName = "公式模板.xlsx";
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        wb.write(response.getOutputStream());
        wb.close();
    }

}
